﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoServey
{
    class FileOperator
    {
        /// <summary>
        ///  弹出打开文件对话框，并返回选择的文件路径
        /// </summary>
        /// <param name="sFileFilter"></param>
        /// <returns></returns>
        public string getFilePath(string sFileFilter)
        {
            string sOpenfileName = string.Empty;
            try
            {
                OpenFileDialog pOpenFileDlg = new OpenFileDialog();

                pOpenFileDlg.Filter = sFileFilter;
                if (pOpenFileDlg.ShowDialog() == DialogResult.OK)
                {
                    sOpenfileName = pOpenFileDlg.FileName;
                }
                return sOpenfileName;
            }
            catch (System.Exception ex)
            {
                return sOpenfileName;
            }
        }

        /// <summary>
        /// 创建保存文件对话框
        /// </summary>
        /// <param name="sFileFilter"></param>
        /// <param name="sTitle"></param>
        /// <returns></returns>
        public string getDirectoryPath()
        {
            string sSaveFile = string.Empty;
            try
            {
                FolderBrowserDialog pSaveFileDlg = new FolderBrowserDialog();

                if (pSaveFileDlg.ShowDialog() == DialogResult.OK)
                {
                    sSaveFile = pSaveFileDlg.SelectedPath;
                }
                return sSaveFile;
            }
            catch (System.Exception ex)
            {
                return sSaveFile;
            }

        }

        /// <summary>
        /// 获取路径下所有文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fileList"></param>
        public void getAllFiles(string path, List<string> fileList)
        {
            DirectoryInfo folder = new DirectoryInfo(path);

            FileInfo[] chldFiles = folder.GetFiles("*.xml");
            foreach (FileInfo chlFile in chldFiles)
            {
                fileList.Add(chlFile.FullName);
            }
            DirectoryInfo[] chldFolders = folder.GetDirectories();
            foreach (DirectoryInfo chldFolder in chldFolders)
            {
                getAllFiles(chldFolder.FullName, fileList);
            }
        }

        /// <summary>
        ///    拷贝文件夹
        /// </summary>
        /// <param name="srcdir"></param>
        /// <param name="desdir"></param>
        /// <returns></returns>
        public bool MoveDirectory(string srcdir, string desdir)
        {
            try
            {
                string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);
                string desfolderdir = desdir + "\\" + folderName;
                if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
                {
                    desfolderdir = desdir + folderName;
                }
                string[] filenames = Directory.GetFileSystemEntries(srcdir);
                foreach (string file in filenames) //遍历所有的文件和目录
                {
                    if (Directory.Exists(file)) //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    {
                        string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                        if (!Directory.Exists(currentdir))
                        {
                            Directory.CreateDirectory(currentdir);
                        }
                        MoveDirectory(file, desfolderdir);
                    }
                    else  //否则直接copy文件
                    {
                        string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);
                        srcfileName = desfolderdir + "\\" + srcfileName;
                        if (!Directory.Exists(desfolderdir))
                        {
                            Directory.CreateDirectory(desfolderdir);
                        }
                        File.Move(file, srcfileName);
                    }
                }//foreach
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

    }
}
